<template>
    <div class="adminRole">
        <el-form ref="formRef" :inline="true" :model="form" class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]">
            <el-form-item label="货位编码：" prop="code">
                <el-input v-model="form.code" placeholder="请输入货位编码" clearable class="!w-[220px]" />
            </el-form-item>
            <el-form-item label="状态：" prop="state">
                <el-select v-model="form.state" clearable placeholder="请选择状态" class="!w-[220px]">
                    <el-option v-for="item in statusSelect" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="货架：" prop="rackid">
                <el-select v-model="form.rackid" clearable placeholder="请选择货架" class="!w-[220px]">
                    <el-option v-for="item in rackselect" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="对应站点：" prop="stationName">
                <el-input v-model="form.stationName" placeholder="请输入对应站点" clearable class="!w-[160px]" />
                <!-- <el-select v-model="form.stationName" clearable placeholder="请选择对应站点" class="!w-[160px]">
          <el-option v-for="item in stationSelect" :key="item.stationid" :label="item.stationname" :value="item.stationid" />
        </el-select> -->
            </el-form-item>
            <el-form-item label="货位类型：" prop="attribute">
                <el-select v-model="form.attribute" clearable filterable placeholder="请选择货位类型" class="!w-[220px]">
                    <el-option v-for="item in attributeSelect" :key="item.Id" :label="item.Name" :value="item.Code" />
                </el-select>
            </el-form-item>
            <el-form-item label="层数：" prop="layer">
                <el-input-number v-model="form.layer" controls-position="right" placeholder="请输入层数"
                    class="!w-[220px]" />
            </el-form-item>
            <el-form-item label="列数：" prop="column">
                <el-input-number v-model="form.column" controls-position="right" placeholder="请输入列数"
                    class="!w-[220px]" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :icon="useRenderIcon(Search)" :loading="loading" @click="onSearch">
                    搜索
                </el-button>
                <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
                    重置
                </el-button>
            </el-form-item>
        </el-form>

        <PureTableBar title="货位管理" :columns="newColumns" @refresh="onSearch" v-if="tableHtml">
            <template #title>
                <div>
                    <span class="font-bold truncate">
                        货位管理
                    </span>
                    <el-dropdown trigger="click" placement="right" :hide-on-click="false"
                        @visible-change="handleCheckboxChange">
                        <el-button type="primary" plain :icon="useRenderIcon(EpArrowDown)" size="small">
                            筛选
                        </el-button>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item v-for="item in simplifiedColumns" :key="item.id">
                                    <el-checkbox v-model="item.isShow" :label="item.label" size="small"
                                        :tabindex="item.id" v-if="item.id !== 66" />
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </template>
            <template #buttons>
                <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="openDialog()">
                    新增货位
                </el-button>
                <el-button type="primary" :icon="useRenderIcon(signOutBold)" @click="exportExcel()">
                    导出
                </el-button>
                <!-- <el-button type="warning" :icon="useRenderIcon(clearOutlined)" @click="clearrackslocation()">
                     清空出库货位
                </el-button> -->
            </template>
            <template v-slot="{ size, dynamicColumns }">
                <pure-table ref="tableRef" adaptive align-whole="center" row-key="Id" showOverflowTooltip
                    table-layout="auto" :loading="loading" :size="size" :data="dataList" :columns="dynamicColumns"
                    :pagination="pagination" :paginationSmall="size === 'small' ? true : false" :header-cell-style="{
                        background: 'var(--el-fill-color-light)',
                        color: 'var(--el-text-color-primary)'
                    }" @page-size-change="handleSizeChange" @page-current-change="handleCurrentChange">
                    <template #operation="{ row }">
                        <!-- <el-button class="reset-margin" link type="success" :size="size" :icon="useRenderIcon(loadingIcon)"
              @click="initialize(row)">
              初始化
            </el-button> -->
                        <!-- <el-button class="reset-margin" link type="success" :size="size" :icon="useRenderIcon(outboxIcon)" @click="addAppointOut(row)" >
              出库
            </el-button>
            <el-button class="reset-margin" link type="warning" :size="size" :icon="useRenderIcon(outlineDelete)" @click="addDelteLoad(row)">
              删除货载
            </el-button> -->
                        <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
                            @click="openDialog('编辑', row)">
                            编辑
                        </el-button>
                        <el-button class="reset-margin" link type="danger" :size="size" :icon="useRenderIcon(Delete)"
                            @click="delLocation(row)">
                            删除
                        </el-button>
                        <!-- <el-button class="reset-margin" link :type="row.status == 2 ? 'success' : 'danger'" :size="size"
              :icon="useRenderIcon(gearBold)" @click="handleStatus(row)">
              {{ row.status == 2 ? '启用' : '禁用' }}
            </el-button> -->
                    </template>
                </pure-table>
            </template>
        </PureTableBar>
    </div>
</template>

<script lang='ts' setup>
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useDept } from "./utils/hook";
import { ref, nextTick, watch, onMounted } from 'vue'

// iconify-icons
import AddFill from "@iconify-icons/ri/add-circle-line";
import EditPen from "@iconify-icons/ep/edit-pen";
import outlineDelete from '@iconify-icons/ic/outline-delete';
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import outboxIcon from '@iconify-icons/quill/outbox';
import Delete from '@iconify-icons/ep/delete';
import clearOutlined from '@iconify-icons/ant-design/clear-outlined';
import loadingIcon from '@iconify-icons/icon-park/loading';
import signOutBold from '@iconify-icons/ph/sign-out-bold';
import EpArrowDown from "@iconify-icons/ep/arrow-down-bold";
import Check from "@iconify-icons/ep/check";

const formRef = ref(null)

const {
    columns,
    loading,
    dataList,
    openDialog,
    handleStatus,
    form,
    onSearch,
    resetForm,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    statusSelect,
    attributeSelect,
    rackselect,
    categorySelect,
    addAppointOut,
    addDelteLoad,
    stationSelect,
    clearrackslocation,
    initialize,
    exportExcel,
    delLocation,
    agvModel
} = useDept();

// const simplifiedColumns = ref(JSON.parse(localStorage.getItem('racklocationsColumns')) || [
//     { id: 1, isShow: true, label: "货位编码" },
//     { id: 2, isShow: true, label: "货位类型" },
//     { id: 3, isShow: true, label: "对应站点" },
//     { id: 4, isShow: true, label: "进入高度" },
//     { id: 5, isShow: true, label: "抬升后高度" },
//     { id: 6, isShow: true, label: "状态" },
//     { id: 7, isShow: true, label: "货架" },
//     { id: 8, isShow: true, label: "层数" },
//     { id: 9, isShow: true, label: "列数" },
//     { id: 10, isShow: true, label: "左右距离(取)" },
//     { id: 11, isShow: true, label: "左右距离(放)" },
//     { id: 12, isShow: true, label: "前后距离" },
//     { id: 13, isShow: true, label: "旋转方向" },
//     { id: 14, isShow: true, label: "AGV型号" },
//     { id: 15, isShow: true, label: "货位前站点" },
//     { id: 60, isShow: true, label: "备注" },
//     { id: 66, isShow: true, label: "操作" }
// ])
const simplifiedColumns = ref(JSON.parse(localStorage.getItem('racklocationsColumns')) || columns.map(item => {
    return {
        id: item.id,
        isShow: true,
        label: item.label
    }
}))
const newColumns = ref(simplifiedColumns.value.filter(item => item.isShow).map(item => {
    return columns.find(item2 => item2.id === item.id)
}))
const tableHtml = ref(true);
const handleCheckboxChange = (isVisible) => {
    if (!isVisible) {
        newColumns.value = simplifiedColumns.value.filter(item => item.isShow).map(item => {
            return columns.find(item2 => item2.id === item.id)
        })
        tableHtml.value = false;
        nextTick(() => {
            tableHtml.value = true;
            localStorage.setItem('racklocationsColumns', JSON.stringify(simplifiedColumns.value));
        });
    }
}

</script>

<style lang="scss" scoped>
.search-form {
    :deep(.el-form-item) {
        margin-bottom: 12px;
    }
}
</style>